<?php

namespace app\api\model;
use think\Model;
use think\facade\Request;
class Coupons extends Model{
	public function select(){
		  $couponRes = db('coupons')->where('del_status', 0)->where('end_date', '>', time())->order('id desc')->select();
        $data = Request::param();

         if(isset($data['uid'])&&!empty($data['uid'])&&$data['uid'] != 'undefined'){
         	$uid = $data['uid'];
        foreach ($couponRes as $k => $v) {
        	$status = db('couponsTaken')->field('use_status')->where('uid',$uid)
        	->where('coupons_id',$v['id'])->find();
             
        	$couponRes[$k]['use_status'] = $status['use_status'];

        }     

    }

        return_json(200,'成功',$couponRes);
	}


	public function take($data){
            $uid = $data['uid'];	
        
            $couponsId = $data['coupons_id'];
            $couponsTaken = db('couponsTaken')->where(['uid'=>$uid, 'coupons_id'=>$couponsId])->find();
            $couponsInfo = db('coupons')->find($couponsId);
           
            if($couponsTaken){
               // return_json(200,'失败','领取失败,您已经领取过该券了!每人限领取 1 张');
                 return_json(200,'失败',false);
            }else{
                if($couponsInfo['cp_num'] > $couponsInfo['cp_taken_num']){
                    $data['uid'] = $uid;
                    $data['coupons_id'] = $couponsId;
                    $data['taken_time'] = time();
                    db('couponsTaken')->insert($data);
                    db('coupons')->where('id', $couponsId)->setInc('cp_taken_num');
                   // return_json(200,'成功','领取成功！感谢您的参与，祝您购物愉快~');
                   return_json(200,'成功',true); 
                }else{
                    // return_json(400,'失败','对不起，您来晚了，该优惠券已被领光！');
                    return_json(400,'失败',false);
                }

            }
        
    }


    public function detail($uid){
        $res = db('couponsTaken')->field('coupons_id')->where('uid',$uid)->where('use_status',0)->select();
        $coupons = [];
        foreach ($res as $k => $v) {
           
           $coupons[] = self::where('id',$v['coupons_id'])->find();
        }
        return_json(200,'成功',$coupons);
    }

    public function det($id){
        $res = self::where('id',$id)->find();
        if($res){
            return_json(200,'成功',$res);
        }else{
            return_json(400,'失败',false);
        }
    }



    public function sel($pagenum,$pagesize){
         $map =[];
  $keywords = Request::param('query');
  if(!empty($keywords)){
    //条件2
   $map[] = ['cp_name','like','%'.$keywords.'%'];
}
        $res = db('coupons')->where($map)->page($pagesize,$pagenum)->select();
        $count = self::count();
        $info = array(
          'data'=>$res,
          'total'=>$count
      );
        if($info){
            return_json(200,'成功',$info);
        }else{
              return_json(400,'失败',false);
        }
    }


    public function edit($id,$data){
        $res = self::where('id',$id)->update($data);
        if($res){
            return_json(200,'成功',true);
        }else{
              return_json(400,'失败',false);
        }
    }

    public function add($data){
        $res = self::save($data);
        if($res){
            return_json(200,'成功',true);
        }else{
              return_json(400,'失败',false);
        }  
    }

    public function del($id){
         $res = self::where('id',$id)->delete();
        if($res){
            return_json(200,'成功',true);
        }else{
              return_json(400,'失败',false);
        }  
    }

     public function delAll($ids){
     $res = self::destroy($ids);
     if($res){
           return_json(200,'成功',true);
       }else{
           return_json(400,'失败',false);
       }
   }
	
}

?>